package com.myoffice.action;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import com.myoffice.biz.UserInfoBiz;
import com.myoffice.pageModel.DataGrid;
import com.myoffice.pageModel.UserInfo;
import com.myoffice.pageModel.base.Json;
import com.myoffice.pageModel.base.SessionInfo;
import com.myoffice.util.IpUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 用户Action
 * 
 * @author jie
 * 
 */
@ParentPackage("basePackage")
@Namespace("")
@Action("userAction")
public class UserAction extends BaseAction implements ModelDriven<UserInfo> {
	private UserInfo userInfo = new UserInfo();

	public UserInfo getModel() {
		return userInfo;
	}

	private UserInfoBiz userInfoBiz = null;

	public UserInfoBiz getUserInfoBiz() {
		return userInfoBiz;
	}

	@Autowired
	public void setUserInfoBiz(UserInfoBiz userInfoBiz) {
		this.userInfoBiz = userInfoBiz;
	}

	public void reg() {
		Json j = new Json();
		userInfo = this.userInfoBiz.reg(userInfo);
		if (userInfo != null) {

			j.setSuccess(true);
			j.setMsg("你注册成功！请登录！");
		} else {
			j.setMsg("注册失败！用户已存在！");
		}

		super.writeJson(j);
	}

	public void add() {
		Json j = new Json();
		try {
			userInfo = this.userInfoBiz.reg(userInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (userInfo != null) {
			j.setSuccess(true);
			j.setObj(userInfo);
			j.setMsg("添加成功！请登录！");
		} else {
			j.setMsg("添加失败！用户已存在！");
		}

		super.writeJson(j);
	}

	/**
	 * 登录
	 */
	public void login() {
		SessionInfo sessionInfo = new SessionInfo();
		Json j = new Json();
		userInfo = this.userInfoBiz.login(userInfo);
		if (userInfo != null) {
			sessionInfo.setUserId(userInfo.getId());
			sessionInfo.setLoginName(userInfo.getName());
			sessionInfo.setLoginPassword(userInfo.getPassword());
			sessionInfo.setIp(IpUtil.getIpAddr(ServletActionContext
					.getRequest()));
			// ServletActionContext.getRequest().getSession().setAttribute(ResourceUtil.getSessionInfoName(),
			// sessionInfo);
			// sessionInfo.setAuthIds(userInfo.getAuthIds());
			// sessionInfo.setAuthNames(u.getAuthNames());
			// sessionInfo.setRoleIds(u.getRoleIds());
			// sessionInfo.setRoleNames(u.getRoleNames());
			// sessionInfo.setAuthUrls(u.getAuthUrls());
			this.userInfoBiz.saveLoginLog(sessionInfo, j);
			j.setSuccess(true);
			j.setObj(sessionInfo);
			j.setMsg("你登录成功！");
		} else {
			j.setMsg("你密码或用户名称错误，登录失败！");
		}

		super.writeJson(j);
	}

	public void datagrid() {
		DataGrid d = new DataGrid();
		d = this.userInfoBiz.datagrid(userInfo);
		super.writeJson(d);
	}

	/**
	 * 删除用户
	 */
	public void remove() {
		userInfoBiz.remove(userInfo.getIds());
		Json j = new Json();
		j.setMsg("删除成功");
		j.setSuccess(true);
		super.writeJson(j);
	}

	/**
	 * 编辑或者更新用户信息
	 */
	public void edit() {
		userInfo = userInfoBiz.edit(userInfo);
		Json j = new Json();
		j.setMsg("编辑成功");
		j.setSuccess(true);
		j.setObj(userInfo);
		super.writeJson(j);
	}
	/**
	 * 操作用户 退出重新登录
	 */		    
	public void doNotNeedSession_logout() {
		ServletActionContext.getRequest().getSession().invalidate();
		Json j = new Json();
		j.setSuccess(true);
		super.writeJson(j);
	}

}







